extends Sprite2D

class_name Card

var isDragging: bool = false
var startPos: Vector2

var data: CardData

func setData(data: CardData):
	self.data = data
	
	self.texture = data.image
	$desc.text = data.cardDesc
	$name.text = data.cardName
	
	$hpNum.text = str(data.hp)
	
	self.data.card = self

func getAttackDelay():
	var speed = data.speed
	return 100.0 / speed

func isSelectCard():
	var pos = get_local_mouse_position()
	
	if pos.x < 150 and pos.y < 200 and pos.x > -150 and pos.y > -200:
		return true
	return false

func getDraginShadow():
	var playerGroup = get_node("/root/Scene/player_group")
	for i in range(7):
		var shadow = playerGroup.get_child(i)
		var pos = shadow.get_local_mouse_position()
		
		if pos.x < 150 and pos.y < 200 and pos.x > -150 and pos.y > -200:
			return i
	
	return -1


func getShadowPosition(i):
	var playerGroup = get_node("/root/Scene/player_group")
	return playerGroup.get_child(i).position


func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
			var mouse_pos = get_global_mouse_position()
			if isSelectCard():
				isDragging = true
				
				startPos = position
				
				z_index = 999
				
		else:
			if isDragging:
				isDragging = false
				z_index = 0
				
				var index = getDraginShadow()
				if index == -1:
					position = startPos
				else:
					var playerGroup: PlayerGroup = get_node("/root/Scene/player_group")
					reparent(playerGroup)
					
					playerGroup.addCard(self)
					
					position = getShadowPosition(index)
					
					var handGroup = get_node("/root/Scene/HandGroup")
					handGroup.removeCard(self)
					
					data.upLine.call()
					
	elif event is InputEventMouseMotion:
		if isDragging:
			position = position + event.relative


var bulletPrefab = load("res://prefab/bullet.tscn")
var hurtLabelPrefab = load("res://prefab/hurt_label.tscn")

func runHurtAnim(damage: int):
	var hurtLabel = hurtLabelPrefab.instantiate()
	self.add_child(hurtLabel)
	
	var randX = randf() * 200 - 100
	var randY = randf() * 200 - 100
	hurtLabel.position += Vector2(randX, randY)
	
	hurtLabel.text = "-" + str(damage)
	
	updateHpBar()
	
var cureLabelPrefab = load("res://prefab/cure_label.tscn")
func runCureAnim(damage: int):
	var cureLabel: Label = cureLabelPrefab.instantiate()
	self.add_child(cureLabel)
	
	var randX = randf() * 200 - 100
	var randY = randf() * 200 - 100
	cureLabel.position += Vector2(randX, randY)
	
	cureLabel.text = "+" + str(damage)
	
	updateHpBar()
	
	
var buffLabelPrefab = load("res://prefab/buff_label.tscn")
func runBuffAnim(text: String):
	var buffLabel: Label = buffLabelPrefab.instantiate()
	self.add_child(buffLabel)
	
	buffLabel.position += Vector2(0, -100)
	buffLabel.text = text
	
	buffLabel.z_index = 5;
	
	


func updateHpBar():
	$hpBar.value = data.hp * 100.0 / data.maxHp;
	$hpNum.text = str(data.hp)


func runAttackAnim(target: Card, endCall: Callable):
	var bullet: Bullet = bulletPrefab.instantiate()
	
	var targetPos = target.global_position
	var fromPos = self.global_position
	
	var bulletLayer = get_node("/root/Scene/BulletLayer")
	bulletLayer.add_child(bullet)
	
	bullet.position = fromPos
	bullet.targetPos = targetPos
	
	bullet.scale = Vector2.ONE * 0.3
	
	var diff = targetPos - fromPos
	var angle = diff.angle()
	bullet.rotation = angle
	
	bullet.endCall = endCall
	

func runDeadAnim():
	
	if data.isMonster:
		var monsterGroup: MonsterGroup = get_node("/root/Scene/monster_group")
		monsterGroup.removeCard(self)
	else:
		var playerGroup: PlayerGroup = get_node("/root/Scene/player_group")
		playerGroup.removeCard(self)
	
	self.queue_free()


func _process(delta: float) -> void:
	pass


	
	
	
	
